package com.liruan.rest.dao;

import java.util.Map;

import org.apache.ibatis.annotations.Lang;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.liruan.rest.dao.lang.BaseDriver;
import com.liruan.rest.po.UserAccountPO;

public interface UserInfoMapper
{
	public static final String USER_ACCOUNT_TABLE = "user_account";
	
	@Lang(BaseDriver.class)
	@Results(value = { @Result(column = "id", property = "id"), @Result(column = "account", property = "account"),
			@Result(column = "password", property = "password") })
	@SelectProvider(type = SQLProvider.class, method = "findUserProvider")
	UserAccountPO findUser(@Param("account") String account, @Param("password") String password);

	static class SQLProvider
	{
		private static final Logger LOGGER = LoggerFactory.getLogger(SQLProvider.class);

		static String currentMethodName()
		{
			return Thread.currentThread().getStackTrace()[2].getMethodName();
		}

		public String findUserProvider(Map<String, Object> param)
		{
			SQL sql = new SQL();
			sql.SELECT("id, account, password").FROM(USER_ACCOUNT_TABLE).WHERE("account=#{account}").WHERE(
					"password=#{password}");

			LOGGER.info("方法:[{}] 生成的SQL:[{}]", currentMethodName(), sql);
			return sql.toString();
		}
	}
}
